Page({
  data: {
    controll: "none",
    type: "编辑",
    isEditor: true,
    addImg: "../../img/add.png",
    subImg: "../../img/sub.png",
    lists: [{
      id: "1",
      photo: "../../img/demoimage1.jpg",
      name: "光明牛奶",
      price: "4.1",
      count: 1,
      c_color: "gray"
    }, {
      id: "2",
      photo: "../../img/demoimage2.jpg",
      name: "光明豆浆",
      price: "5.1",
      count: 1,
      c_color: "gray"
    }, {
      id: "3",
      photo: "../../img/demoimage3.jpg",
      name: "光明饮料",
      price: "3.1",
      count: 1,
      c_color: "gray"
    }],
    totalPri: 0,
    chooseList: [],
  },
  submit: function() {
    wx.navigateTo({
      url: 'MakeSure/MakeSure'
    })
  },
  editor: function() {
    if (this.data.type == "编辑") {
      this.setData({
        controll: "inline-block",
        type: "完成",
        addImg: "../../img/gray-add.png",
        subImg: "../../img/gray-sub.png"
      })
    } else {
      console.log(this.data.chooseList);
      for (var i = 0; i < this.data.lists.length; i++) {
        for (var j = 0; j < this.data.chooseList.length; j++) {
          if (this.data.lists[i].id == this.data.chooseList[j]) {
            this.data.lists.splice(i, 1);
          }
        }
      }
      this.saveMoney(),
        this.setData({
          controll: "none",
          type: "编辑",
          addImg: "../../img/add.png",
          subImg: "../../img/sub.png",
          lists: this.data.lists
        })
    }
  },
  cancel: function() {
    this.setData({
      controll: "none",
      type: "编辑",
      addImg: "../../img/add.png",
      subImg: "../../img/sub.png"
    })
  },
  subAction: function(event) {

    for (var i = 0; i < this.data.lists.length; i++) {
      if (this.data.lists[i].id == event.target.dataset.id) {
        this.data.lists[i].count--;
        this.data.totalPri = (-parseFloat(this.data.lists[i].price) + parseFloat(this.data.totalPri)).toFixed(2);
        if (this.data.lists[i].count == 0) {
          this.data.lists.splice(i, 1);
        }
      }
      this.setData({
        lists: this.data.lists,
        totalPri: this.data.totalPri,

      })
    }
  },
  addAction: function(event) {
    for (var i = 0; i < this.data.lists.length; i++) {
      if (this.data.lists[i].id == event.target.dataset.id) {
        this.data.lists[i].count++;
        this.data.totalPri = (parseFloat(this.data.lists[i].price) + parseFloat(this.data.totalPri)).toFixed(2);
      }
      this.setData({
        lists: this.data.lists,
        totalPri: this.data.totalPri
      })
    }
  },
  saveMoney: function() {
    var total = 0;
    for (var i = 0; i < this.data.lists.length; i++) {
      total += this.data.lists[i].count * parseFloat(this.data.lists[i].price).toFixed(2);
    }
    this.setData({
      totalPri: total.toFixed(2)
    })
  },
  choose: function(event) {

    for (var i = 0; i < this.data.lists.length; i++) {
      if (this.data.lists[i].id == event.target.dataset.id) {
        if (this.data.lists[i].c_color == "gray") {
          this.data.lists[i].c_color = "red";
          this.data.chooseList.push(this.data.lists[i].id);
        } else if (this.data.lists[i].c_color == "red") {
          this.data.lists[i].c_color = "gray";
          for (var j = 0; j < this.data.chooseList.length; j++) {
            if (this.data.chooseList[j] == event.target.dataset.id) {
              this.data.chooseList.splice(j, 1);
            } else {

            }
          }
        }
      }
    }
    console.log(this.data.chooseList);
    this.setData({
      lists: this.data.lists,
      chooseList: this.data.chooseList
    })
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.saveMoney();
  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  }

})